package com.open.shitou.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.open.shitou.R;
import com.open.shitou.adapter.HomeInfoAdapter;
import com.open.shitou.bean.Advertisement;
import com.open.shitou.uilts.Consts;
import com.open.shitou.uilts.GlideImageLoader;
import com.open.shitou.view.RefreshListView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by hailongqiu on 2018/2/26.
 */

public class HomeFragment extends BaseFragment {

    public static HomeFragment newInstance(String s) {
        HomeFragment nearbyFragment = new HomeFragment();
        return nearbyFragment;
    }

    Banner mBannerView;
    @BindView(R.id.data_lv)
    RefreshListView mPullRefreshListView;

    HomeInfoAdapter mHomeInfoAdapter;
    List<Advertisement> mADList = new ArrayList<Advertisement>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        AutoUtils.auto(view);
        ButterKnife.bind(this, view);
        //
        initListViewHeaderView();
        initPullRefreshListView();
        initBannerView();
        initBannerDatas();
        return view;
    }

    private void initListViewHeaderView() {
        // 添加头信息.
        final View headView = View.inflate(getActivity(), R.layout.include_home_head_info,
                null);
        AutoUtils.auto(headView);
        mBannerView = headView.findViewById(R.id.banner);
        mPullRefreshListView.addHeaderView(headView, null, false);
    }

    /*
    * 初始化pullRefresh.
    */
    private void initPullRefreshListView() {
        // 初始化pullrefreshlistview
        mPullRefreshListView.setPageSize(Consts.LIMIT_PAGE_NUM);
        //
        mHomeInfoAdapter = new HomeInfoAdapter(getActivity(), mADList);
        mPullRefreshListView.setAdapter(mHomeInfoAdapter);
        // 连接item单击事件.
        mPullRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
            }
        });
        // 加载更多.
        mPullRefreshListView.setOnLoadListener(new RefreshListView.OnLoadListener() {
            @Override
            public void onLoad() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                            mPullRefreshListView.onLoadComplete();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
        // 刷新
        mPullRefreshListView.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
            @Override
            public void toRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1500);
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mPullRefreshListView.onRefreshComplete();
                                }
                            });
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }

    private void initBannerView() {
        //设置banner样式
//        banner.setBannerStyle(BannerConfig.NUM_INDICATOR_TITLE);
        mBannerView.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        mBannerView.setImageLoader(new GlideImageLoader());
        //设置banner动画效果
        mBannerView.setBannerAnimation(Transformer.Default);
        //设置自动轮播，默认为true
        mBannerView.isAutoPlay(true);
        //设置轮播时间
        mBannerView.setDelayTime(2888);
        //设置指示器位置（当banner模式中有指示器时）
//        banner.setIndicatorGravity(BannerConfig.RIGHT);
    }

    private void initBannerDatas() {
        BmobQuery<com.open.shitou.bean.Banner> query = new BmobQuery<com.open.shitou.bean.Banner>();
        query.findObjects(new FindListener<com.open.shitou.bean.Banner>() {
            @Override
            public void done(List<com.open.shitou.bean.Banner> list, BmobException e) {
                if (null != list) {
                    final List<String> images = new ArrayList<>();
                    final List<String> texts = new ArrayList<>();
                    for (com.open.shitou.bean.Banner b : list) {
                        images.add(b.getImg().getFileUrl());
                        texts.add(b.getTitle());
                    }
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mBannerView.setImages(images);
                            mBannerView.setBannerTitles(texts);
                            mBannerView.start();
                        }
                    });
                }
            }
        });
    }

    /**
     * 进入地址选择 界面.
     */
    @OnClick(R.id.addr_llyt)
    public void onAddrClick() {
        Toast.makeText(getActivity(), "fdshjf", Toast.LENGTH_SHORT).show();
    }

    /**
     * 进入石头商品搜索 界面.
     */
    @OnClick(R.id.search_llyt)
    public void onSearchClick() {
        Toast.makeText(getActivity(), "fdshjf", Toast.LENGTH_SHORT).show();
    }

}
